/**
 * 
 */
define([],function(){
	webUtils.nameSpace("wiseMen.platform.adminDivSelector");
	
	wiseMen.platform.adminDivSelector.adminDivTree = function (params) {
	    params = $.extend({
	    	seqAble:false
	    }, params);
	    var rootData=[];
	    
	    if(params.initAdminDivs){
	    	if($.isArray(params.initAdminDivs)){
	    		$.each(params.initAdminDivs,function(i,n){
	    			n=$.extend({
	    				isParent: true,
	    	            open: true,
	    	            id:'-1'
	    			},n)
	    			rootData.push(n);
		    	});
	    	}else{
	    		params.initAdminDivs.text=params.initAdminDivs.name;
	    		var n=$.extend({
    				isParent: true,
    	            open: true,
    	            id:'-1'
    			},params.initAdminDivs);
	    		rootData.push(n);
	    	}
	    }else{
	    	rootData.push({
	            text: "行政区划",
	            id: "-1",
	            isParent: true,
	            open: true
	        });
	    }
	    var adminDivTree = $.fn.zTree.init(params.target, {
	        treeId: webUtils.uuid(),
	        async: {
	            enable: true,
	            url: ctx + "/adminDivAct/queryTreeNode.do",
	            autoParam: ["id=pid","code=pcode"],
	            dataFilter: function (treeId, parentNode, responseData) {
	                if (responseData) {
	                    $.each(responseData, function (i, n) {
	                        if (n.childrenSize > 0) {
	                            n.isParent = true;
	                        }
	                    });
	                }
	                return responseData;
	            }
	        },
	        data: {
	            key: {
	                name: "text"
	            }
	        },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                drag:params.seqAble?{
                    isCopy: false
                }:undefined
            },
	        callback: {
	            onClick: params.onClick,
	            onDrop: params.seqAble?function (e, treeId, treeNodes, targetNode, moveType, isCopy) {
                    if (treeNodes && treeNodes.length > 0) {
                        var treeNode = treeNodes[0];
                        var pNode = adminDivTree.getNodeByTId(treeNode.parentTId);
                        var ids = [];
                        $.each(pNode.children, function (i, n) {
                            ids.push(n.id);
                        });
                        $.post(ctx + "/adminDivAct/seqNodes.do", {ids: ids.join(","), pid: pNode.id}, function (data) {
                            if (data.success) {
                            } else {
                                webUtils.error(data.msg);
                            }
                        }, "json");
                    }
                }:null 
	        }
	    }, rootData);
	    
	    var roots=adminDivTree.getNodesByParam("id", "-1");
	    $.each(roots,function(i,n){
	    	 adminDivTree.reAsyncChildNodes(n, "refresh");
	    });
	    
	    return adminDivTree;
	};
	return wiseMen.platform.adminDivSelector.adminDivTree;
});